"""
去除标注的默认值
"""
import json
import os

# 通过循环与判断来找出via的json标注文件
for root, dirs, files in os.walk(r"D:\file\postgrad\experiment\silver_spare_dataset\videos_labelframes_resize", topdown=False):
    for file in files:
        # via的json标注文件以_proposal.json结尾
        if file.endswith("_proposal.json"):
            jsonPath = os.path.join(root, file)

            try:
                # 读取标注文件
                with open(jsonPath, 'r', encoding='utf-8') as f:
                    viaJson = json.load(f)  # 读取整个JSON文件

                # 遍历并修改metadata中的每个标注
                for metadata in viaJson.get('metadata', {}):
                    # 清空标注中的选项值
                    viaJson['metadata'][metadata]["av"] = {'1': ''}

                # 修改后的文件名
                newName = file.split(".")[0] + '_s.json'
                newPath = os.path.join(root, newName)

                # 写入修改后的标注文件
                with open(newPath, 'w', encoding='utf-8') as f2:
                    json.dump(viaJson, f2, ensure_ascii=False, indent=2)

            except json.JSONDecodeError as e:
                print(f"JSON解码错误: {e}")
            except Exception as e:
                print(f"文件处理时发生错误: {e}")
